package com.gz.EuchreU.Controls;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.modifier.ease.EaseStrongOut;

import com.badlogic.gdx.math.Vector2;
import com.gz.EuchreU.Contants.CardValues;
import com.gz.EuchreU.Contants.GameOptions;
import com.gz.EuchreU.Contants.Suits;
import com.gz.EuchreU.Contants.UI;
import com.gz.EuchreU.Helpers.Textures;
import com.gz.EuchreU.Sprite.Card;

public abstract class MovingMenuEntity extends Entity{

	protected Vector2 mOnScreenLocation;
	protected Vector2 mOffScreenLocation = new Vector2(UI.getWidth()/2, UI.getHeight()*2);
	
	protected Sprite backGround;
	
	protected boolean isOnScreen = false;
	
	float currentScale = 1;
	float currentRotation = 0;
	
	protected boolean mActive = false;
	
	public MovingMenuEntity(float pOnScreenX, float pOnScreenY, float pWidth, boolean pUseBackground, Vector2 pOffScreen){
		mOffScreenLocation = pOffScreen;
		Init(pOnScreenX, pOnScreenY, pWidth, pUseBackground);
	}
	
	public MovingMenuEntity(float pOnScreenX, float pOnScreenY, float pWidth, boolean pUseBackground){
		Init(pOnScreenX, pOnScreenY, pWidth, pUseBackground);
	}
	
	private void Init(float pOnScreenX, float pOnScreenY, float pWidth, boolean pUseBackground){
		mOnScreenLocation = new Vector2(pOnScreenX,pOnScreenY);
		mOnScreenLocation = new Vector2();
		
		setRotationCenter(pWidth/2, pWidth/2);
		
		setPosition(mOffScreenLocation.x, mOffScreenLocation.y);
		
		backGround = new Sprite(pOnScreenX,pOnScreenY,pWidth,UI.getHeight() - pOnScreenY,UI.getTextures().getTexture(Textures.TABLE_BLACK_TEXTURE ));
		
		if(pUseBackground){
			attachChild(backGround);
		}
	}
	
	public void Show(){
		MoveOnScreen();
		isOnScreen = true;
	}
	
	public void Hide(){
		MoveOffScreen();
		isOnScreen = false;
	}
	
	protected float mOnScreenDuration = 2;
	protected float mOnScreenRotation = 0;
	protected float mOnScreenScale = 1;
	
	private void MoveOnScreen(){
		
		
		
		ParallelEntityModifier mod = new ParallelEntityModifier(
				new MoveModifier(mOnScreenDuration, getX(), mOnScreenLocation.x, getY(), mOnScreenLocation.y, EaseStrongOut.getInstance()),
				new RotationModifier(mOnScreenDuration,currentRotation,mOnScreenRotation,EaseStrongOut.getInstance()),
				new ScaleModifier(mOnScreenDuration, currentScale, mOnScreenScale)
		);
		
		clearEntityModifiers();
		this.registerEntityModifier(mod);
		
		currentRotation = mOnScreenRotation;
		currentScale = mOnScreenScale;
		
		mActive = true;
	}
	
	protected float mOffScreenScale = .25f;
	protected float mOffScreenDuration = 3;
	protected float mOffScreenRotation = 720;
	
	private void MoveOffScreen(){
		
		
		
		ParallelEntityModifier mod = new ParallelEntityModifier(
				new MoveModifier(mOffScreenDuration, getX(), mOffScreenLocation.x, getY(), mOffScreenLocation.y, EaseStrongOut.getInstance()),
				new RotationModifier(mOffScreenDuration,currentRotation,mOffScreenRotation,EaseStrongOut.getInstance()),
				new ScaleModifier(mOffScreenDuration, currentScale, mOffScreenScale)
		);
		
		clearEntityModifiers();
		this.registerEntityModifier(mod);
		
		currentRotation = mOffScreenRotation;
		currentScale = mOffScreenScale;
		
		mActive = false;
	}
	
	public Text GetText(float pX, float pY, String pText){
		return new Text(pX,pY,UI.getTextures().getFont(),pText);
	}
	
	public Card GetAceOfSpades(final float pX, final float pY, final float pWidth){
		return new Card(pX,pY,pWidth,Suits.SPADES,CardValues.ACE){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){		
						this.setDisabled(!this.getDisabled());
				}
				return true;
			}
		};
	}
}
